﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;

namespace ClientRegister
{
    public partial class LoginForm : Form
    {
        //Khoi tao ham chay dong bo hoa du lieu
        public void Sync(object state)
        {
            SynchronizeData sync = new SynchronizeData();
            sync.syncData();
        }

        public LoginForm()
        {
            InitializeComponent();

            Application.Exit();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //Tao doi tuong callBack
            TimerCallback callBack = new TimerCallback(Sync);
            System.Threading.Timer callTimer = new System.Threading.Timer(callBack, null, 0, 3000);

            //sync.transDataToServer();
            //sync.GetDataForInsertMethodWithID(sync.EmployTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.EmployTb, sync.updatecol);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int role = 0;

            if (txtID.Text == "" || txtPassword.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ ID và Password");
                txtID.Focus();
                return;
            }
            else
            {
                string id = txtID.Text.ToString();
                string pass = txtPassword.Text;
                EmployeesDataProvider emp = new EmployeesDataProvider();
                SqlDataReader reader = emp.Login(id, pass);
                if(reader.Read())
                    role = int.Parse(reader.GetValue(0).ToString());
                switch (role)
                {
                    case 1:
                        EmployeeManageForm nvqtform = new EmployeeManageForm();
                        nvqtform.ID = txtID.Text.ToString();
                        nvqtform.Password = txtPassword.Text;
                        nvqtform.Name = reader.GetValue(1).ToString();
                        nvqtform.Show();
                        this.Hide();
                        break;

                    case 2:
                        EmployeeRegisForm nvdkform = new EmployeeRegisForm();
                        nvdkform.ID = txtID.Text.ToString();
                        nvdkform.Password = txtPassword.Text;
                        nvdkform.Name = reader.GetValue(1).ToString();
                        nvdkform.Show();
                        this.Hide();
                        break;

                    case 3:
                        EmployeeAcaAdminExeForm nvgvform = new EmployeeAcaAdminExeForm();
                        nvgvform.ID = txtID.Text.ToString();
                        nvgvform.Password = txtPassword.Text;
                        nvgvform.Name = reader.GetValue(1).ToString();
                        nvgvform.Show();
                        this.Hide();
                        break;
                    default:
                        MessageBox.Show("ID hoặc Password không đúng. Vui lòng nhập lại");
                        break;
                }
            }
        }


    }
}
